﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
using ZF.Model;
namespace ZF.WS.Admin
{
    public class Article : ZF.BLL.Page.GMPage
    {
        private int _Type = 0;

        [PageArgument(Key = "tid", CanNull = false)]
        public int ArticleTypeId { get { return (int)ArticleType; } set { ArticleType = value.Convert<ArticleType>(); } }

        public ArticleType ArticleType { get; set; }

        public virtual string ListPage { get { return "/Person/expert/Articlelist.aspx"; } }
        public virtual string ManagePage { get { return "/Person/expert/ArticleManage.aspx"; } }
        public virtual string CTitle { get; set; }
    }
    public partial class ArticleManage : Article
    {
    
        
        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int ArticleId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {

            //this.ddlParams.DataSource = DictionaryConfig.MTypeJsonList.Select(p => new
            //{
            //    Key = p.MType.MerchandiseTypeId,
            //    Text = p.MType.Name,
            //});
            //this.ddlParams.DataBind();
            //this.lvMTypeParams.DataSource = DictionaryConfig.MTypeJsonList;
            //this.lvMTypeParams.DataBind();
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {
                

                var art = new ZF.Model.E_Article()
                {
                    AddTime = DateTime.Now,
                    Cotent = this.fckInfo.Value,
                    Tag = this.tbTag.Text,
                    Title = this.tbTitle.Text,
                    MemberId = CurrentUser.MemberId,
                    Type = (int)ArticleType,
                };
                SiteInfo.CheckFilterWords(art);
                try
                {
                   

                    dc.E_Article.InsertOnSubmit(art);
                    dc.SubmitChanges();
               
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }


                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = "提交成功",
                    DoPage = this.ListPage + "?tid=" + this.ArticleTypeId.ToString(),
                }).Alert();
                return;
                string msgKey = CTitle;

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一篇{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续发布{0}", msgKey), Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage + "?tid=" + ((int)this.ArticleType).ToString()).Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var art = dc.E_Article.FirstOrDefault(p => p.MemberId == CurrentUser.MemberId && p.ArticleId == this.ArticleId);
                if (art == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbTag.Text = art.Tag;
                this.tbTitle.Text = art.Title;
                this.fckInfo.Value = art.Cotent;
            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var art = dc.E_Article.FirstOrDefault(p => p.MemberId == CurrentUser.MemberId && p.ArticleId == this.ArticleId);
                    if (art == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    art.Cotent = this.fckInfo.Value;
                    art.Tag = this.tbTag.Text;
                    art.Title = this.tbTitle.Text;
        
                    try
                    {
                        SiteInfo.CheckFilterWords(art);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;


                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Message = "修改成功",
                         Link= WarnLink.Current,
                    }).Alert();
                    return;
                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一篇{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage + "?tid=" + ((int)this.ArticleType).ToString()).Alert();

                }


            }

        }

    
    }
 
}
